/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.adapters.impl;

import cz.muni.fi.pa165.bookingmanager.adapters.CustomerAdapter;
import cz.muni.fi.pa165.bookingmanager.dto.CustomerDTO;
import cz.muni.fi.pa165.bookingmanager.entities.Customer;
import cz.muni.fi.pa165.bookingmanager.exceptions.IllegalOrphanException;
import cz.muni.fi.pa165.bookingmanager.exceptions.NonexistentEntityException;
import cz.muni.fi.pa165.bookingmanager.exceptions.PreexistingEntityException;
import cz.muni.fi.pa165.bookingmanager.services.CustomerService;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Jiří Holuša
 */
@Stateless
public class CustomerAdapterImpl implements CustomerAdapter {

    @EJB
    private CustomerService customerService;

    @Override
    public void createCustomer(CustomerDTO customer) throws IllegalOrphanException, PreexistingEntityException {
        Customer customerEntity = AdaptersHelper.customerDTOToEntity(customer);
        customerService.createCustomer(customerEntity);
        customer.setId(customerEntity.getId());
    }

    @Override
    public void updateCustomer(CustomerDTO customer) throws NonexistentEntityException, IllegalOrphanException, PreexistingEntityException, Exception {
        Customer customerEntity = AdaptersHelper.customerDTOToEntity(customer);
        customerService.updateCustomer(customerEntity);
    }

    @Override
    public void deleteCustomer(CustomerDTO customer) throws NonexistentEntityException, IllegalOrphanException {
        Customer customerEntity = AdaptersHelper.customerDTOToEntity(customer);
        customerService.deleteCustomer(customerEntity);
    }

    @Override
    public CustomerDTO getCustomerById(Long customerId) {
        Customer customerEntity = customerService.getCustomerById(customerId);
        CustomerDTO customer = AdaptersHelper.customerEntityToDTO(customerEntity);
        return customer;
    }

    @Override
    public List<CustomerDTO> findCustomersByName(String customer) {
        List<Customer> customerEntity = customerService.findCustomersByName(customer);
        List<CustomerDTO> customers = new ArrayList<CustomerDTO>();
        for (Customer cust : customerEntity) {
            customers.add(AdaptersHelper.customerEntityToDTO(cust));
        }
        return customers;
    }

    @Override
    public List<CustomerDTO> findAllCustomers() {
        List<Customer> customerEntity = customerService.findAllCustomers();
        List<CustomerDTO> customers = new ArrayList<CustomerDTO>();
        for (Customer cust : customerEntity) {
            customers.add(AdaptersHelper.customerEntityToDTO(cust));
        }
        return customers;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }
}
